package socket

import (
    "code.google.com/p/go.net/websocket"
    "net"
   // "os"
    "fmt"
)

func JKParseIp(ipstr string) net.IP {
    name := ipstr
    addr := net.ParseIP(name)
    if addr == nil {  // fail
        fmt.Println("Invalid address")
    } else {
        fmt.Println("The address is ", addr)
    }
    return addr
}

func GetIpAddrs(host string) ([]string, error) {
    return net.LookupHost(host)
}

func GetHosts(addr string) ([]string, error) {
    return net.LookupAddr(addr)
}

func GetAddrsHosts(ah string) (string, error) {
    addr, err := GetIpAddrs(ah)
    result := ""
    if err == nil {
        for i := 0; i < len(addr); i++ {
            result += addr[i] + "\n"
        }
    }
    host, err := GetHosts(ah)
    if err == nil {
        for i := 0; i < len(host); i++ {
            result += host[i] + "\n"
        }
    }
    return result, err
}

func WebSocket(ws *websocket.Conn) {
    var err error

    for {
        var reply string
        if err = websocket.Message.Receive(ws, &reply); err != nil {
            fmt.Println("Can't receive")
            break
        }
        fmt.Println("Received back from client: "+reply)
        msg := "REceived: "+reply
        fmt.Println("Sending to client: " + msg)
        if err = websocket.Message.Send(ws, msg); err != nil {
            fmt.Println("Can't send")
            break
        }
    }
}
